博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring4+mybaits3整合—项目Demo
阅读量:5904 次
发布时间:2019-06-19

本文共 20960 字,大约阅读时间需要 69 分钟。

hot3.png

项目下载地址:

项目下载地址2:

这里就简单的粘一下部分重要代码,方便以后查看

一、框架简介

       这个demo采用目前最新的技术Spring4+Mybatis3+maven +freemarker+Bootstrap3构建的系统部分底层框架。目前以初具模型,可以直接用在项目上。

      系统运行环境:tomcat7+ 、JDK7+、MySql 5.5+

二、系统部分代码

      这里只粘贴部分代码,不做过多的解说,如果想了解具体内容请加QQ:864060165 详谈。

     1、web.xml

      

SSR Web Application
httpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
httpMethodFilter
Spring MVC Dispatcher Servlet
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
Spring MVC Dispatcher Servlet
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-config.xml
Spring MVC Dispatcher Servlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
Spring MVC Dispatcher Servlet
/
30
index.jsp
2、Spring-config.xml

3、Spring-mvc.xml

json=application/json xml=application/xml
4、sqlMapConfig.xml

5、ecache配置

6、数据库映射文件

dic_id as dicId, dic_name as dicName, dic_value as dicValue, dic_group as dicGroup, dic_type as dicType, dic_order as dicOrder, dic_status as dicStatus, dic_parent_id as dicParentId
and dic_id = #{dicId}
and dic_name = #{dicName}
and dic_value = #{dicValue}
and dic_group = #{dicGroup}
and dic_type = #{dicType}
and dic_order = #{dicOrder}
and dic_status = #{dicStatus}
and dic_parent_id = #{dicParentId}
and dic_name like CONCAT("%",#{dicNameLike},"%")
and dic_group like CONCAT("%",#{dicGroupLike},"%")
order by ${sorting}
limit #{offset}, #{limit}
delete from sys_dictionary where dic_id = #{dicId}
delete from sys_dictionary
insert into sys_dictionary( dic_id, dic_name, dic_value, dic_group, dic_type, dic_order, dic_status, dic_parent_id ) values ( #{dicId}, #{dicName}, #{dicValue}, #{dicGroup}, #{dicType}, #{dicOrder}, #{dicStatus}, #{dicParentId} )
update sys_dictionary
dic_name = #{dicName},
dic_value = #{dicValue},
dic_group = #{dicGroup},
dic_type = #{dicType},
dic_order = #{dicOrder},
dic_status = #{dicStatus},
dic_parent_id = #{dicParentId},
where dic_id = #{dicId}
update sys_dictionary set dic_name = #{dicName}, dic_value = #{dicValue}, dic_group = #{dicGroup}, dic_type = #{dicType}, dic_order = #{dicOrder}, dic_status = #{dicStatus}, dic_parent_id = #{dicParentId} where dic_id = #{dicId}
7、Mybatis通用BaseDao实现

package com.viathink.frame.core.dao.impl;import java.util.List;import java.util.Map;import org.apache.commons.lang3.StringUtils;import org.apache.ibatis.session.RowBounds;import org.apache.ibatis.session.SqlSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageImpl;import org.springframework.data.domain.Pageable;import org.springframework.transaction.annotation.Transactional;import org.springframework.util.Assert;import com.viathink.frame.core.dao.BaseDao;import com.viathink.frame.core.dao.constants.SqlId;import com.viathink.frame.core.dao.domain.Identifiable;import com.viathink.frame.core.exception.DaoException;import com.viathink.frame.core.utils.BeanUtils;import com.viathink.frame.core.utils.UUIDUtils;/** * 基础Dao接口实现类,实现改类的子类必须设置泛型类型 * @author LiuJunGuang * @date 2014年3月3日下午1:02:31 */public abstract class BaseDaoImpl
implements BaseDao
{ @Autowired(required = true) protected SqlSession sqlSessionTemplate; public static final String SQLNAME_SEPARATOR = "."; /** * @fields sqlNamespace SqlMapping命名空间 */ private String sqlNamespace = getDefaultSqlNamespace(); /** * 获取泛型类型的实体对象类全名 * @author LiuJunGuang * @return * @date 2014年3月3日下午6:17:46 */ protected String getDefaultSqlNamespace() { Class
genericClass = BeanUtils.getGenericClass(this.getClass()); return genericClass == null ? null : genericClass.getName(); } /** * 获取SqlMapping命名空间 * @author LiuJunGuang * @return SqlMapping命名空间 * @date 2014年3月4日上午12:33:15 */ public String getSqlNamespace() { return sqlNamespace; } /** * 设置SqlMapping命名空间。 以改变默认的SqlMapping命名空间, * 不能滥用此方法随意改变SqlMapping命名空间。 * @author LiuJunGuang * @param sqlNamespace SqlMapping命名空间 * @date 2014年3月4日上午12:33:17 */ public void setSqlNamespace(String sqlNamespace) { this.sqlNamespace = sqlNamespace; } /** * 将SqlMapping命名空间与给定的SqlMapping名组合在一起。 * @param sqlName SqlMapping名 * @return 组合了SqlMapping命名空间后的完整SqlMapping名 */ protected String getSqlName(String sqlName) { return sqlNamespace + SQLNAME_SEPARATOR + sqlName; } /** * 生成主键值。 默认使用{@link UUIDUtils#create()}方法 * 如果需要生成主键,需要由子类重写此方法根据需要的方式生成主键值。 * @param entity 要持久化的对象 */ protected String generateId() { return UUIDUtils.create(); } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectOne(java.io.Serializable) */ @Override public
V selectOne(T query) { Assert.notNull(query); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT), params); } catch (Exception e) { throw new DaoException(String.format("查询一条记录出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectById(java.io.Serializable) */ @Override public
V selectById(String id) { Assert.notNull(id); try { return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_BY_ID), id); } catch (Exception e) { throw new DaoException(String.format("根据ID查询对象出错!语句:%s", getSqlName(SqlId.SQL_SELECT_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectList(java.io.Serializable) */ @Override public
List
selectList(T query) { try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), params); } catch (Exception e) { throw new DaoException(String.format("查询对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectAll() */ @Override public
List
selectAll() { try { return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT)); } catch (Exception e) { throw new DaoException(String.format("查询所有对象列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectMap(java.io.Serializable, java.lang.String) */ @Override public
Map
selectMap(T query, String mapKey) { Assert.notNull(mapKey, "[mapKey] - must not be null!"); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), params, mapKey); } catch (Exception e) { throw new DaoException(String.format("查询对象Map时出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /** * 设置分页 * @param pageInfo 分页信息 * @return SQL分页参数对象 */ protected RowBounds getRowBounds(Pageable pageable) { RowBounds bounds = RowBounds.DEFAULT; if (null != pageable) { bounds = new RowBounds(pageable.getOffset(), pageable.getPageSize()); } return bounds; } /** * 获取分页查询参数 * @param query 查询对象 * @param pageable 分页对象 * @return Map 查询参数 */ protected Map
getParams(T query, Pageable pageable) { Map
params = BeanUtils.toMap(query, getRowBounds(pageable)); if (pageable != null && pageable.getSort() != null) { String sorting = pageable.getSort().toString(); params.put("sorting", sorting.replace(":", "")); } return params; } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) */ @Override public
List
selectList(T query, Pageable pageable) { try { return sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable)); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectPageList(com.viathink.core.dao.domain.Identifiable, org.springframework.data.domain.Pageable) */ @Override public
Page
selectPageList(T query, Pageable pageable) { try { List
contentList = sqlSessionTemplate.selectList(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable)); return new PageImpl
(contentList, pageable, this.selectCount(query)); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectMap(com.viathink.core.dao.domain.Identifiable, java.lang.String, org.springframework.data.domain.Pageable) */ @Override public
Map
selectMap(T query, String mapKey, Pageable pageable) { try { return sqlSessionTemplate.selectMap(getSqlName(SqlId.SQL_SELECT), getParams(query, pageable), mapKey); } catch (Exception e) { throw new DaoException(String.format("根据分页对象查询列表出错!语句:%s", getSqlName(SqlId.SQL_SELECT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectCount() */ @Override public Long selectCount() { try { return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT)); } catch (Exception e) { throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#selectCount(java.io.Serializable) */ @Override public Long selectCount(T query) { try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT), params); } catch (Exception e) { throw new DaoException(String.format("查询对象总数出错!语句:%s", getSqlName(SqlId.SQL_SELECT_COUNT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#insert(java.io.Serializable) */ @Override public void insert(T entity) { Assert.notNull(entity); try { if (StringUtils.isBlank(entity.getId())) entity.setId(generateId()); sqlSessionTemplate.insert(getSqlName(SqlId.SQL_INSERT), entity); } catch (Exception e) { throw new DaoException(String.format("添加对象出错!语句:%s", getSqlName(SqlId.SQL_INSERT)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#delete(java.io.Serializable) */ @Override public int delete(T query) { Assert.notNull(query); try { Map
params = BeanUtils.toMap(query); return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE), params); } catch (Exception e) { throw new DaoException(String.format("删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteById(java.io.Serializable) */ @Override public int deleteById(String id) { Assert.notNull(id); try { return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE_BY_ID), id); } catch (Exception e) { throw new DaoException(String.format("根据ID删除对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteAll() */ @Override public int deleteAll() { try { return sqlSessionTemplate.delete(getSqlName(SqlId.SQL_DELETE)); } catch (Exception e) { throw new DaoException(String.format("删除所有对象出错!语句:%s", getSqlName(SqlId.SQL_DELETE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateById(java.io.Serializable) */ @Override public int updateById(T entity) { Assert.notNull(entity); try { return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID), entity); } catch (Exception e) { throw new DaoException(String.format("根据ID更新对象出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateByIdSelective(java.io.Serializable) */ @Override @Transactional public int updateByIdSelective(T entity) { Assert.notNull(entity); try { return sqlSessionTemplate.update(getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE), entity); } catch (Exception e) { throw new DaoException(String.format("根据ID更新对象某些属性出错!语句:%s", getSqlName(SqlId.SQL_UPDATE_BY_ID_SELECTIVE)), e); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#deleteByIdInBatch(java.util.List) */ @Override @Transactional public void deleteByIdInBatch(List
idList) { if (idList == null || idList.isEmpty()) return; for (String id : idList) { this.deleteById(id); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#updateInBatch(java.util.List) */ @Override @Transactional public void updateInBatch(List
entityList) { if (entityList == null || entityList.isEmpty()) return; for (T entity : entityList) { this.updateByIdSelective(entity); } } /* (non-Javadoc) * @see com.viathink.core.dao.BaseDao#insertInBatch(java.util.List) */ @Override public void insertInBatch(List
entityList) { if (entityList == null || entityList.isEmpty()) return; for (T entity : entityList) { this.insert(entity); } }}
9、通用BaseControllerImpl实现

package com.viathink.frame.core.web.controller;import java.util.Arrays;import org.apache.commons.lang3.ArrayUtils;import org.apache.commons.lang3.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.web.PageableDefault;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import com.viathink.frame.core.dao.BaseService;import com.viathink.frame.core.dao.domain.Identifiable;import com.viathink.frame.core.web.domain.ControllerPath;import com.viathink.frame.core.web.domain.Result;import com.viathink.frame.core.web.domain.Result.Status;/** * 基础控制器接口实现类 * @author LiuJunGuang * @date 2014年3月5日下午12:03:13 */public abstract class BaseControllerImpl
implements BaseController
{ private Logger log = LoggerFactory.getLogger(BaseControllerImpl.class); /** * @fields path 页面路径信息 */ protected ControllerPath path = new ControllerPath(this.getClass()); /** * 获取基础的服务 * @return BaseService */ protected abstract BaseService
getBaseService(); @Override @ResponseBody @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) public Result deleteList(String[] ids) { if (ArrayUtils.isEmpty(ids)) { log.error("未设置批量删除对象的ID号!对象:{}", path.getEntityName()); return new Result(Status.ERROR, "没有传入要删除的ID号数组!"); } try { getBaseService().deleteByIdInBatch(Arrays.asList(ids)); } catch (Exception e) { log.error("批量删除对象失败!对象:" + path.getEntityName(), e); return new Result(Status.ERROR, "批量删除失败!"); } return new Result(Status.OK, ids.length); } @Override @ResponseBody @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) public Result deleteOne(@PathVariable("id") String id) { if (StringUtils.isBlank(id)) { log.error("要删除的ID号为null或空字符串!对象:{}", path.getEntityName()); return new Result(Status.ERROR, "没有传入要删除的ID号!"); } int count = getBaseService().deleteById(id); if (count == 0) return new Result(Status.ERROR, "要删除的记录不存在!"); log.debug("成功删除{}个对象,id:{},对象:{}", count, id, path.getEntityName()); return new Result(Status.OK, count); } @Override @RequestMapping(method = RequestMethod.POST) public ModelAndView addOne(T entity) { getBaseService().insert(entity); return new ModelAndView(path.getRedirectListPath()); } @Override @RequestMapping(value = "/add", method = RequestMethod.GET) public ModelAndView addView() { return new ModelAndView(path.getAddViewPath()); } @Override @RequestMapping(method = RequestMethod.GET) public ModelAndView selectList(Q query, @PageableDefault Pageable pageable) { Page
page = getBaseService().queryPageList(query, pageable); ModelAndView mav = new ModelAndView(path.getListViewPath(), "page", page); mav.addObject("query", query); return mav; } @Override @RequestMapping(value = "/{id}", method = RequestMethod.GET) public ModelAndView viewOne(@PathVariable("id") String id) { Object obj = getBaseService().queryById(id); return new ModelAndView(path.getOneViewPath(), path.getEntityName(), obj); } @Override @ResponseBody @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) public Result editOne(T entity) { getBaseService().updateById(entity); return new Result(Status.OK, entity); } @Override @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET) public ModelAndView editView(@PathVariable("id") String id) { Object obj = getBaseService().queryById(id); return new ModelAndView(path.getEditViewPath(), path.getEntityName(), obj); }}
10、列表结果页面:

<#include "/common/common.ftl"><@pnotify/>字典列表	<#include "/common/navbar.ftl"> 	<#-- 内容开始 -->	
<#-- 查询条件 -->
查询条件
名称
<#-- 结果列表 -->
字典列表
<#list page.content as result>
序号 名称 类型 排序 状态 操作
${result_index+1} ${result.dicName} ${result.dicValue} ${result.dicGroup} ${result.dicType.label} ${result.dicOrder} ${result.dicStatus.label}
<@tablePage/>
<#-- 内容结束 --> <#include "/common/footer.ftl"> <@modal/>
11、编辑页面:

<#assign base=request.contextPath />	
三、运行结果图:

项目下载地址:

项目下载地址2:

转载于:https://my.oschina.net/java2010/blog/356421

你可能感兴趣的文章
Windows server 2003 FTP 全攻略(二)
查看>>
tomcat6.0启动报错
查看>>
Android Studio第三十七期 - webview与js交互
查看>>
用户管理脚本之删除用户——Delete_user.sh
查看>>
sql server日期时间函数
查看>>
哈哈!终于完成LFS,庆祝!庆祝!
查看>>
Excel打印图片时,图片上加的文字无法显示
查看>>
硬盘安装Fedora 8
查看>>
把 MapXtreme for Java 迁移到 Tomcat 5.5 上
查看>>
Java中log4j控制写入日志开关
查看>>
DWR+freemarker+commons.mail 实现模板定制动态邮件发送
查看>>
科普系列之-让你的本本使用WLAN
查看>>
Liferay 如何加入自定义的js库文件
查看>>
LYNC2010 标准版的安装
查看>>
PD设计中表名过长,自动生成的主外键名截取的问题
查看>>
Microsoft Dynamics CRM 2013 电子邮件路由器安装程序 介绍
查看>>
Android:数据库增删改查、SQLite、SQLiteOpenHelper、openOrCreateDatabase
查看>>
007 content for
查看>>
使用spring boot devtools不要多此一举加try...catch
查看>>
[奇思怪想]浏览器插件之静音盒子
查看>>